/*
 * @Author: lixiangqun
 * @Date: 2025-10-29 22:47:01
 * @LastEditors: lixiangqun
 * @LastEditTime: 2025-10-30 07:00:08
 * @Description: 会话store
 * @FilePath: /electrconVue/src/stores/conversation.ts
 */
import { defineStore } from "pinia";
import { ConversationProps } from "../types";
import { db } from "../db";

export interface ConversationStore {
  items: ConversationProps[];
  selectedId: number;
}

export const useConversationStore = defineStore("conversation", {
  state: (): ConversationStore => {
    return {
      items: [],
      selectedId: -1,
    };
  },
  actions: {
    async fetchConversation() {
      this.items = await db.conversations.toArray();
    },
    async createConversation(data: Omit<ConversationProps, "id">) {
      const newId = await db.conversations.add(data);
      this.items.push({ ...data, id: newId });
      return newId;
    },
  },
  getters: {
    getConversationById: (state) => (id: number) => {
      return state.items.find((item) => item.id === id);
    },
  },
});
